package com.kvest.ula.android;

import java.util.ArrayList;

import com.kvest.ula.R;
import com.kvest.ula.activities.CitiesListActivity;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

public class CitiesListAdapter extends ArrayAdapter<CitiesListActivity.ListItem>
{
	private ArrayList<CitiesListActivity.ListItem> items;
	private LayoutInflater inflater;
	private int viewResourceId;
	
	public CitiesListAdapter(Context context, int viewResourceId, ArrayList<CitiesListActivity.ListItem> items) 
    {
        super(context, viewResourceId, items);
        this.inflater = LayoutInflater.from(context);
        this.items = items;
        this.viewResourceId = viewResourceId;
    }
	
	@Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
    	View view = convertView;
    	if (view == null) 
        {
        	view = this.inflater.inflate(viewResourceId, null);
        }
    	
        CitiesListActivity.ListItem element = items.get(position);
        
        CheckBox checked = (CheckBox)view.findViewById(R.id.city_item_checked);
        if (element != null && checked != null)
        {
        	checked.setChecked(element.selected);
        }
        
        TextView name = (TextView) view.findViewById(R.id.city_item_text);   
        if (element != null && name != null) 
        {
        	name.setText(Html.fromHtml(element.name));
        }
        return view;
    }
}
